package com.applications.codemash.utility.parser;

import java.util.ArrayList;
import java.util.List;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.RootElement;
import android.util.Xml;

import com.applications.codemash.core.*;

public class SessionFeedParser extends BaseFeedParser<Session>
{
	public SessionFeedParser(String requestUrl)
	{
		super(requestUrl);
	}

	@Override
	public List<Session> parse()
	{
		final List<Session> sessions = new ArrayList<Session>();
		RootElement root = new RootElement(Constants.SESSIONS_ROOT_ELEMENT);
		Element sessionElement = root.getChild(Constants.SESSION_ELEMENT_NAME);
		final Session currentSession = new Session(sessionElement);
		
		sessionElement.setEndElementListener(new EndElementListener() {
			@Override
			public void end()
			{
				sessions.add(currentSession.copy());
			}
		});
		
		try
		{
			Xml.parse(getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
		}
		catch (Exception ex)
		{
			throw new RuntimeException(ex);
		}
	
		return sessions;
	}
}
